package com.wyl.salary.ui.page

import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.dialog
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.wyl.salary.ui.viewmodel.SalaryViewModel

@Composable
fun MainPage(viewModel: SalaryViewModel = viewModel()) {
    val navController = rememberNavController()
//    val vmOwner = remember {
//        navController.getBackStackEntry("MainPage")
//    }
//    val vm : SalaryViewModel = viewModel(vmOwner)
    NavHost(navController = navController, startDestination = Router.SalaryListPage) {
        composable(Router.SalaryListPage) {
            SalaryListPageWithViewModel(
                navController,
                viewModel = viewModel
            )
        }
        composable(Router.AddSalaryPage) {
            AddSalaryPageWithViewModel(navController) {
                viewModel.getAllSalary()
            }
        }
        composable(
            "${Router.EditSalaryPage}{${Router.EditSalaryPage_Param_Id}}", arguments = listOf(
                navArgument(Router.EditSalaryPage_Param_Id) { type = NavType.LongType }
            )
        ) { entry ->
            entry.arguments?.getLong(Router.EditSalaryPage_Param_Id)?.let {
                EditSalaryPageWithViewModel(navController, sid = it) {
                    viewModel.getAllSalary()
                }
            }
        }
        composable(Router.SetDurationPage) {
            SetDurationPageWithViewModel(navController) {
                viewModel.getAllSalary()
            }
        }
        composable(Router.DefaultSalaryPage) {
            DefaultSalaryPageWithViewModel(navController)
        }
        dialog(Router.EditSalaryPageDeleteDialog) {
            DeleteDialog(navController)
        }
    }
}
